package com.ruoyi.qiushi.service.equipment.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qiushi.domain.equipment.QiushiDevice;
import com.ruoyi.qiushi.domain.equipment.QiushiDeviceBindRecord;
import com.ruoyi.qiushi.mapper.equipment.QiushiDeviceBindRecordMapper;
import com.ruoyi.qiushi.mapper.equipment.QiushiDeviceMapper;
import com.ruoyi.qiushi.service.equipment.IQiushiDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 设备列Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class QiushiDeviceServiceImpl implements IQiushiDeviceService {
    @Autowired
    private QiushiDeviceMapper qiushiDeviceMapper;
    @Autowired
    private QiushiDeviceBindRecordMapper qiushiDeviceBindRecordMapper;

    /**
     * 查询设备列
     *
     * @param id 设备列主键
     * @return 设备列
     */
    @Override
    public QiushiDevice selectQiushiDeviceById(Long id) {
        return qiushiDeviceMapper.selectQiushiDeviceById(id);
    }

    /**
     * 查询设备列列表
     *
     * @param qiushiDevice 设备列
     * @return 设备列
     */
    @Override
    public List<QiushiDevice> selectQiushiDeviceList(QiushiDevice qiushiDevice) {
        return qiushiDeviceMapper.selectQiushiDeviceList(qiushiDevice);
    }

    @Override
    public List<QiushiDevice> selectReportDevice() {
        return qiushiDeviceMapper.selectReportDevice();
    }

    @Override
    public QiushiDevice getDeviceByDeviceNo(String deviceNo){
        return qiushiDeviceMapper.getDeviceByDeviceNo(deviceNo);
    }

    /**
     * 新增设备列
     *
     * @param qiushiDevice 设备列
     * @return 结果
     */
    @Override
    public int insertQiushiDevice(QiushiDevice qiushiDevice) {
        qiushiDevice.setCreateTime(DateUtils.getNowDate());
        return qiushiDeviceMapper.insertQiushiDevice(qiushiDevice);
    }

    /**
     * 修改设备列
     *
     * @param qiushiDevice 设备列
     * @return 结果
     */
    @Override
    public int updateQiushiDevice(QiushiDevice qiushiDevice) {
        qiushiDevice.setUpdateTime(DateUtils.getNowDate());

        qiushiDeviceMapper.updateQiushiDevice(qiushiDevice);

        int status = Integer.parseInt(qiushiDevice.getStatus());

        QiushiDeviceBindRecord qiushiDeviceBindRecord = new QiushiDeviceBindRecord();
        qiushiDeviceBindRecord.setDeviceId(qiushiDevice.getId());
        qiushiDeviceBindRecord.setFamilyId(qiushiDevice.getFamilyId());
        qiushiDeviceBindRecord.setType(String.valueOf(status ^ 1 ^ 2));
        qiushiDeviceBindRecord.setCreateTime(new Date());
        qiushiDeviceBindRecord.setUpdateTime(new Date());
        qiushiDeviceBindRecordMapper.insertQiushiDeviceBindRecord(qiushiDeviceBindRecord);

        if (status == 1) qiushiDeviceMapper.unbind(qiushiDevice.getId());
        return 1;
    }

    /**
     * 删除设备列信息
     *
     * @param id 设备列主键
     * @return 结果
     */
    @Override
    public int deleteQiushiDeviceById(Long id) {
        return qiushiDeviceMapper.deleteQiushiDeviceById(id);
    }
}
